package com.hsmpay.mobile.service.account.impl;

import com.hsmpay.common.pojo.BasePojo;
import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.common.util.NumberUtil;
import com.hsmpay.mobile.dao.organization.OrgPreDepositDao;
import com.hsmpay.mobile.dao.trans.AccountDetailsDao;
import com.hsmpay.mobile.dao.trans.TransOrderDao;
import com.hsmpay.mobile.dao.trans.TransPriorSettleDao;
import com.hsmpay.mobile.dao.trans.cardcer.CardCerDao;
import com.hsmpay.mobile.dao.user.UserMoneyDao;
import com.hsmpay.mobile.pojo.organization.OrgPreDeposit;
import com.hsmpay.mobile.pojo.trans.AccountDetails;
import com.hsmpay.mobile.pojo.trans.TransOrder;
import com.hsmpay.mobile.pojo.trans.TransPriorSettle;
import com.hsmpay.mobile.pojo.trans.cardcer.CardCer;
import com.hsmpay.mobile.pojo.user.UserMoney;
import com.hsmpay.mobile.service.account.CashMoneyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 账务管理 提现  服务接口实现类 cashMoneyService
 *
 * @author 颜铃璋
 *         创建时间：2014-3-19
 */
@Service("cashMoneyService")
public class CashMoneyServiceImpl extends BaseServiceSupport<BasePojo, Long> implements CashMoneyService<BasePojo, Long> {
    @Resource(name = "accountDetailsDao")
    private AccountDetailsDao<AccountDetails, Long> accountDetailsDao;//账户明细
    @Resource(name = "transOrderDao")
    private TransOrderDao<TransOrder, Long> transOrderDao;//交易订单
    @Resource(name = "transPriorSettleDao")
    private TransPriorSettleDao<TransPriorSettle, Long> transPriorSettleDao;//transPriorSettleDao
    @Resource(name = "userMoneyDao")
    private UserMoneyDao<UserMoney, Long> userMoneyDao;
    @Resource(name = "orgPreDepositDao")
    private OrgPreDepositDao<OrgPreDeposit, Long> orgPreDepositDao;
    @Resource(name = "cardCerDao")
    private CardCerDao<CardCer, Long> cardCerDao;

    /**
     * 提现
     *
     * @param transOrder
     * @return 1 提现成功 2 预存款不足
     * @throws Exception
     */
    public synchronized int cashMoney(TransOrder transOrder) throws Exception {
        //修改 金额  防止脏读
        userMoneyDao.updateUserMoneyByUserId(transOrder.getUserId(), 0.0);

        double preDeposit = orgPreDepositDao.getPreDepositByOrgId(transOrder.getTerminalTypeId(), transOrder.getRoleId());
        double allMoney = NumberUtil.roundF2(transOrder.getChargeAmount() + transOrder.getFeeMoney());
        if (preDeposit <= transOrder.getChargeAmount()) {//预存款不足  preDeposit <= 10000 ||
            return 2;
        }
        //修改预存款
        orgPreDepositDao.updateCashMoneyPreDeposit(transOrder.getTerminalTypeId(), transOrder.getRoleId(), transOrder.getChargeAmount(), transOrder.getOrderNum(), transOrder.getUserId());
        //重新判断 预存款  如果存在脏读  将预存款减到负数  那么就直接抛出异常 回滚
        preDeposit = orgPreDepositDao.getPreDepositByOrgId(transOrder.getTerminalTypeId(), transOrder.getRoleId());
        if (preDeposit < 0.0) {
            throw new Exception();
        }

        transOrderDao.insertEntity(transOrder);

        //付款状态
        TransPriorSettle priorSettle = new TransPriorSettle();
        priorSettle.setOrderNum(transOrder.getOrderNum());
        priorSettle.setStatus(1);
        priorSettle.setCollectionType(0);//
        priorSettle.setCreateDate(transOrder.getCreateDate());
        transPriorSettleDao.insertEntity(priorSettle);

        UserMoney userMoney = userMoneyDao.searchUserMoneyByUserId(transOrder.getUserId());
        //3修改用户 余额虚拟币
        UserMoney userMoneyU = new UserMoney();
        userMoneyU.setUserId(transOrder.getUserId());
        userMoneyU.setMoney(0 - (allMoney));
        userMoneyDao.updateUserMoneyByUserId(userMoneyU);
        //修改后  再次查询  如果  金额为负数  那么抛出异常  全部回滚
        userMoney = userMoneyDao.searchUserMoneyByUserId(transOrder.getUserId());
        if (userMoney.getMoney() < 0) {//如果 提现后  不是正数  那个就回滚
            throw new Exception();
        }

        //添加 账单详细
        AccountDetails accountDetails = new AccountDetails();
        accountDetails.setOrderNum(transOrder.getOrderNum());
        accountDetails.setUserId(transOrder.getUserId());
        accountDetails.setBalance(userMoney.getMoney());
        accountDetails.setIncExp(0);//收入1收入 0为支出
        accountDetails.setTransMoney(allMoney);
        accountDetails.setCreateDate(transOrder.getCreateDate());
        accountDetails.setAvailBalance(userMoney.getAvailMoney());
        accountDetailsDao.insertEntity(accountDetails);

        return 1;
    }

    /**
     * 提现 安全
     *
     * @param transOrder
     * @param cardCerId
     * @return 1 提现成功 2 预存款不足
     * @throws Exception
     */
    public int cashMoneySafe(TransOrder transOrder, Long cardCerId) throws Exception {
        //提现
        int flag = cashMoney(transOrder);
        if (flag == 1) {
            //修改原订单提现 编号
            transOrderDao.updateCashOrderByNum(transOrder.getOrgOrderNum(), transOrder.getOrderNum());
            //添加订单编号  修改 未审核
            cardCerDao.updateCardCerOrderNumById(transOrder.getOrderNum(), cardCerId);
        }
        return flag;
    }

    /**
     * 秒到提现 不判断备付金 确保生成提现订单
     *
     * @param transOrder
     * @return 1 提现成功 2 预存款不足
     * @throws Exception
     */
    public int secondCashMoneySafe(TransOrder transOrder) throws Exception {
        //提现
        int flag = secondCashMoney(transOrder);
        if (flag == 1) {
            //修改原订单提现 编号
            transOrderDao.updateCashOrderByNum(transOrder.getOrgOrderNum(), transOrder.getOrderNum());
        }
        return flag;
    }

    /**
     * 微信公众号提现 安全
     *
     * @param transOrder
     * @return 1 提现成功 2 预存款不足
     * @throws Exception
     */
    public int weChartCashMoneySafe(TransOrder transOrder) throws Exception {
        //提现
        int flag = cashMoney(transOrder);
        if (flag == 1) {
            //修改原订单提现 编号
            transOrderDao.updateCashOrderByNum(transOrder.getOrgOrderNum(), transOrder.getOrderNum());
        }
        return flag;
    }

    /**
     * T+1 提现
     * 不需要校验 备付金
     *
     * @param transOrder
     * @return
     * @throws Exception
     */
    public int cashMoneyT1(TransOrder transOrder) throws Exception {
        double allMoney = NumberUtil.roundF2(transOrder.getChargeAmount() + transOrder.getFeeMoney());
        //修改 金额  防止脏读
        userMoneyDao.updateAvailMoneyByUserId(transOrder.getUserId(), 0.0);

        //插入订单
        transOrderDao.insertEntity(transOrder);

        //付款状态
        TransPriorSettle priorSettle = new TransPriorSettle();
        priorSettle.setOrderNum(transOrder.getOrderNum());
        priorSettle.setStatus(1);
        priorSettle.setCollectionType(0);//
        priorSettle.setCreateDate(transOrder.getCreateDate());
        transPriorSettleDao.insertEntity(priorSettle);

        UserMoney userMoney = userMoneyDao.searchUserMoneyByUserId(transOrder.getUserId());
        //3修改用户 余额虚拟币
        UserMoney userMoneyU = new UserMoney();
        userMoneyU.setUserId(transOrder.getUserId());
        userMoneyU.setAvailMoney(0 - (allMoney));
        userMoneyDao.updateUserMoneyByUserId(userMoneyU);
        //修改后  再次查询  如果  金额为负数  那么抛出异常  全部回滚
        userMoney = userMoneyDao.searchUserMoneyByUserId(transOrder.getUserId());
        if (userMoney.getAvailMoney() < 0) {//如果 提现后  不是正数  那个就回滚
            throw new Exception();
        }

        //添加 账单详细
        AccountDetails accountDetails = new AccountDetails();
        accountDetails.setOrderNum(transOrder.getOrderNum());
        accountDetails.setUserId(transOrder.getUserId());
        accountDetails.setBalance(NumberUtil.roundF2(userMoney.getAvailMoney() + userMoney.getMoney()));
        accountDetails.setIncExp(0);//收入1收入 0为支出
        accountDetails.setTransMoney(allMoney);
        accountDetails.setCreateDate(transOrder.getCreateDate());
        accountDetails.setAvailBalance(userMoney.getAvailMoney());
        accountDetailsDao.insertEntity(accountDetails);

        return 1;

    }

    /**
     * 秒到提现 确保生产提现订单 不判断预存款
     *
     * @param transOrder
     * @return 1 提现成功 2 预存款不足
     * @throws Exception
     */
    public synchronized int secondCashMoney(TransOrder transOrder) throws Exception {
        //修改 金额  防止脏读
        userMoneyDao.updateUserMoneyByUserId(transOrder.getUserId(), 0.0);

        double allMoney = NumberUtil.roundF2(transOrder.getChargeAmount() + transOrder.getFeeMoney());

        transOrderDao.insertEntity(transOrder);

        //付款状态
        TransPriorSettle priorSettle = new TransPriorSettle();
        priorSettle.setOrderNum(transOrder.getOrderNum());
        priorSettle.setStatus(1);
        priorSettle.setCollectionType(0);
        priorSettle.setCreateDate(transOrder.getCreateDate());
        transPriorSettleDao.insertEntity(priorSettle);

        //3修改用户 余额虚拟币
        UserMoney userMoneyU = new UserMoney();
        userMoneyU.setUserId(transOrder.getUserId());
        userMoneyU.setMoney(0 - (allMoney));
        userMoneyDao.updateUserMoneyByUserId(userMoneyU);
        //修改后  再次查询  如果  金额为负数  那么抛出异常  全部回滚
        UserMoney userMoney = userMoneyDao.searchUserMoneyByUserId(transOrder.getUserId());
        if (userMoney.getMoney() < 0) {//如果 提现后  不是正数  那个就回滚
            throw new Exception();
        }

        //添加 账单详细
        AccountDetails accountDetails = new AccountDetails();
        accountDetails.setOrderNum(transOrder.getOrderNum());
        accountDetails.setUserId(transOrder.getUserId());
        accountDetails.setBalance(userMoney.getMoney());
        accountDetails.setIncExp(0);//收入1收入 0为支出
        accountDetails.setTransMoney(allMoney);
        accountDetails.setCreateDate(transOrder.getCreateDate());
        accountDetails.setAvailBalance(userMoney.getAvailMoney());
        accountDetailsDao.insertEntity(accountDetails);

        return 1;
    }

}
